package com.senven.branch.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.senven.branch.mapper.BranchMapper;
import com.senven.branch.service.BranchService;
import com.senven.common.entity.Branch;
import com.senven.common.entity.Goods;
import com.senven.common.entity.Order;
import com.senven.common.entity.User;
import com.senven.common.utils.WebUtils;
import com.senven.common.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author wt
 * @Date 2022/8/29 18:00
 * @PackageName:com.senven.branch.service.impl
 * @ClassName: BranchServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private BranchMapper branchMapper;



    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CommonResult findAll() {
        QueryWrapper wrapper = new QueryWrapper();
        List<Branch> list = branchMapper.selectList(wrapper);
        System.out.println(list);
        System.out.println("============");
        System.out.println(list.size());

        //查询所有店铺并查看店铺内是否有菜品上架，若没有任何菜品则不在手机端所有店铺页面展示这个店铺
        //使用递归方法遍历店铺是否存在菜品
        forxh(list);
        System.out.println(list);
        if (list!=null){
            return new CommonResult(2000,"查询成功",list);
        }
        return new CommonResult(5000,"查询失败",null);
    }
    //创建递归方法---遍历没有菜品的店铺并不让其展示到前端页面
    public List<Branch> forxh(List<Branch> list){
        for (int i = 0; i < list.size(); i++) {
            Branch branch = list.get(i);
            Integer branchid = branch.getBranchid();
            String s = String.valueOf(branchid);
            List<Goods> goods = branchMapper.findOrderByBranchid(s);
            if (goods.size()<1){
                list.remove(i);
                forxh(list);
            }
        }
        return list;
    }

    @Override
    public List<Branch> findAl() {
        QueryWrapper wrapper = new QueryWrapper();
        List<Branch> list = branchMapper.selectList(wrapper);
        if (list!=null){
            return list;
        }
        return null;
    }

    @Override
    public CommonResult findById(Integer branchid) {
        Branch branch = branchMapper.selectById(branchid);
        if (branch!=null){
            return new CommonResult(2000,"查询成功",branch);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    @Override
    public Branch findByI(Integer branchid) {
        Branch branch = branchMapper.selectById(branchid);
        if (branch!=null){
            return branch;
        }
        return null;
    }

    @Override
    public CommonResult backfindAll(Integer currentPage, Integer pageSize, Map map) {
        String token = WebUtils.getResult().getHeader("token");
        System.out.println(token);

        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String json = forValue.get(token);
        Map o = JSON.parseObject(json, Map.class);
        System.out.println(o);

        Map user = (Map) o.get("user");

        System.out.println(user);
        Object id = user.get("id");
//        map.put("userid",id);

        String roleid = branchMapper.selecByIdRole(id);
        String branchid = branchMapper.selecBranchid(id);
        map.put("roleid",roleid);
        map.put("branchid",branchid);
        System.out.println(map);
        PageHelper.startPage(currentPage,pageSize);
        List<Branch> list = branchMapper.backFindAll(map);
        if (list!=null){
            PageInfo<Branch> pageInfo = new PageInfo(list);
            return new CommonResult(2000,"查询成功",pageInfo);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    @Override
    public CommonResult orderFindOrderByOrderId(Map map) {

        System.out.println(map);

        Object s =  map.get("branchid");
        String branchid = String.valueOf(s);
//        PageHelper.startPage(currentPage,pageSize);
        List<Goods> list = branchMapper.findOrderByBranchid(branchid);
        if (list!=null){
//            PageInfo<Branch> pageInfo = new PageInfo(list);
            return new CommonResult(2000,"查询成功",list);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    //商家入驻审核通过远程调用添加店铺
    @Override
    public int insertBranch(Branch branch) {
        int insert = branchMapper.insert(branch);
        if (insert>0){
            return insert;
        }
        return -1;
    }

    @Override
    public CommonResult delBranchUser(Branch branch) {
        Integer branchid = branch.getBranchid();
        String userid = branch.getUserid();
        int i = branchMapper.deleteById(branchid);
        int i1 = branchMapper.deleUser(userid);

        return null;
    }
}
